package main

import (
	"log"
	"time"
)

func main() {
	getBestRate()
}

// 获取最优费率
func getBestRate() int {
	defer func(start int64) {	//计算耗时
		log.Printf("getBestRate cost :%d ms", (time.Now().UnixNano() - start) / 1000000)
	}(time.Now().UnixNano())

	rateA := getRateFromA()
	rateB := getRateFromB()
	rateC := getRateFromC()

	bestRate := rateA
	if rateB < bestRate {
		bestRate = rateB
	}
	if rateC < bestRate {
		bestRate = rateC
	}
	return bestRate
}

func getRateFromA() int {
	log.Println("start getRateFromA")
	time.Sleep(2 * time.Second)	//模拟耗时
	log.Println("end getRateFromA")
	return 2
}

func getRateFromB() int {
	time.Sleep(500 * time.Millisecond)	//模拟耗时
	return 5
}

func getRateFromC() int {
	log.Println("start getRateFromC")
	time.Sleep(3 * time.Second)
	log.Println("end getRateFromC")
	return 3
}
